package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._

object Demo3FLinkSqlONKafka {
  def main(args: Array[String]): Unit = {

    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //使用流模型
      .build()

    //创建flink sql的执行环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    bsTableEnv.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age BIGINT,
        |  gender STRING,
        |  clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'student_json',
        | 'properties.bootstrap.servers' = 'master:9092,node1:9092,node2:9092',
        | 'properties.group.id' = 'testGroup',
        | 'format' = 'json',
        | 'json.ignore-parse-errors' = 'true'
        |)
        |
      """.stripMargin)

    /**
      * print ： 用于测试查看输出结果
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table (
        |clazz STRING,
        |c BIGINT
        |)WITH ('connector' = 'print')
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |
        |insert into print_table
        |select clazz,count(1) as c from student group by clazz
        |
      """.stripMargin)


  }

}
